﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Views;
using JiveMessenger.Models;
using JiveMessenger.User;
using JmShared;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace JiveMessenger.ViewModels
{
    public class ViewModelMessageDownloadPage:ViewModelBaseEx
    {
        private INavigationService _navigationService;
        private IClient _currentClient;
        public RelayCommand LoadMessagesFromServerCommand { get; set; }

        private ChatType _chatType;
        public ChatType ChatType
        {
            get { return _chatType; }
            set
            {
                if (_chatType != value)
                {
                    Set(() => ChatType, ref _chatType, value);
                }
            }
        }

        

        private string _chatID;
        public string ChatID
        {
            get { return _chatID; }
            set
            {
                if (_chatID != value)
                {
                    Set(() => ChatID, ref _chatID, value);
                }
            }
        }

        private int _countLimit;
        public int CountLimit
        {
            get { return _countLimit; }
            set
            {
                if (_countLimit != value)
                {
                    Set(() => CountLimit, ref _countLimit, value);
                }
            }
        }

        private int _ageLimit;
        public int AgeLimit
        {
            get { return _ageLimit; }
            set
            {
                if (_ageLimit != value)
                {
                    Set(() => AgeLimit, ref _ageLimit, value);
                }
            }
        }

        private int _downloadAge;
        public int DownloadAge
        {
            get { return _downloadAge; }
            set
            {
                if (_downloadAge != value)
                {
                    Set(() => DownloadAge, ref _downloadAge, value);
                }
            }
        }

        private int _downloadCount;
        public int DownloadCount
        {
            get { return _downloadCount; }
            set
            {
                if (_downloadCount != value)
                {
                    Set(() => DownloadCount, ref _downloadCount, value);
                }
            }
        }


        private string _chatName;
        public string ChatName
        {
            get { return _chatName; }
            set
            {
                if (_chatName != value)
                {
                    Set(() => ChatName, ref _chatName, value);
                }
            }
        }



        public ViewModelMessageDownloadPage(IClient currentClient,INavigationService navService)
        {
            AgeLimit = 40;
            CountLimit = 140;
            DownloadAge = 24;
            DownloadCount = 50;
            _navigationService = navService;
            _currentClient = currentClient;
            LoadMessagesFromServerCommand = new RelayCommand(LoadMessagesFromServerCommandAction, ()=> _currentClient.IsLoggedIn);
        }

        private void LoadMessagesFromServerCommandAction()
        {
            _navigationService.GoBack();
            Task.Run(async () =>
                {
                    await Task.Delay(300);
                    ClientCommands command = ClientCommands.Undefined;
                    if (ChatType == JmShared.ChatType.GroupChat)
                        command = ClientCommands.RequestGroupMessages;
                    else if (ChatType == JmShared.ChatType.PrivateMessage)
                        command = ClientCommands.RequestPrivateMessages;
                    await _currentClient.Connection.SendCommand(new MessageRequest(this.ChatID, this.DownloadCount, this.DownloadAge), command);
                });
        }

        public override void Cleanup()
        {
            SimpleIoc.Default.Unregister<ViewModelMessageDownloadPage>(VmHelper.Locator.ViewModelMessageDownloadKey);
            VmHelper.Locator.ViewModelMessageDownloadKey = System.Guid.NewGuid().ToString();
            base.Cleanup();
        }
    }
}
